home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / rdflib / sparql / Query.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  31.9 KB  |  850 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import types
  5. import sets
  6. from rdflib import URIRef, BNode, Literal
  7. from rdflib.Identifier import Identifier
  8. from rdflib.util import check_subject, list2set
  9. from rdflib.sparql import SPARQLError
  10. from rdflib.sparql.Unbound import Unbound
  11. from rdflib.sparql.sparqlGraph import SPARQLGraph
  12. from rdflib.sparql.graphPattern import GraphPattern
  13.  
  14. class SessionBNode(BNode):
  15.     """
  16.     Special 'session' BNodes.  I.e., BNodes at the query side which refer to 
  17.     BNodes in persistence
  18.     """
  19.     pass
  20.  
  21.  
  22. def _checkOptionals(pattern, optionals):
  23.     """
  24.     The following remark in the SPARQL document is important:
  25.  
  26.     'If a new variable is mentioned in an optional block (as mbox and
  27.     hpage are mentioned in the previous example), that variable can be
  28.     mentioned in that block and can not be mentioned in a subsequent
  29.     block.'
  30.  
  31.     What this means is that the various optional blocks do not
  32.     interefere at this level and there is no need for a check whether
  33.     a binding in a subsequent block clashes with an earlier optional
  34.     block.
  35.  
  36.     This method checks whether this requirement is fulfilled. Raises a
  37.     SPARQLError exception if it is not (the rest of the algorithm
  38.     relies on this, so checking it is a good idea...)
  39.  
  40.     @param pattern: graph pattern
  41.     @type pattern: L{GraphPattern<rdflib.sparql.GraphPattern>}
  42.     @param optionals: graph pattern
  43.     @type optionals: L{GraphPattern<rdflib.sparql.GraphPattern>}
  44.     @raise SPARQLError: if the requirement is not fulfilled
  45.     """
  46.     for i in xrange(0, len(optionals)):
  47.         for c in optionals[i].unbounds:
  48.             if c in pattern.unbounds:
  49.                 continue
  50.             
  51.             if i > 0:
  52.                 for j in xrange(0, i):
  53.                     if c in optionals[j].unbounds:
  54.                         raise SPARQLError('%s is an illegal query string, it appear in a previous OPTIONAL clause' % c)
  55.                     c in optionals[j].unbounds
  56.                 
  57.         
  58.     
  59.  
  60.  
  61. def _variablesToArray(variables, name = ''):
  62.     '''Turn an array of Variables or query strings into an array of query strings. If the \'variables\'
  63.     is in fact a single string or Variable, then it is also put into an array.
  64.  
  65.     @param variables: a string, a unicode, or a Variable, or an array of those (can be mixed, actually). As a special case,
  66.     if the value is "*", it returns None (this corresponds to the wildcard in SPARQL)
  67.     @param name: the string to be used in the error message
  68.     '''
  69.     if isinstance(variables, basestring):
  70.         if variables == '*':
  71.             return None
  72.         return [
  73.             variables]
  74.     isinstance(variables, basestring)
  75.     if isinstance(variables, Unbound):
  76.         return [
  77.             variables.name]
  78.     if type(variables) == list or type(variables) == tuple:
  79.         retval = []
  80.         for s in variables:
  81.             if isinstance(s, basestring):
  82.                 retval.append(s)
  83.                 continue
  84.             isinstance(variables, Unbound)
  85.             if isinstance(s, Unbound):
  86.                 retval.append(s.name)
  87.                 continue
  88.             raise SPARQLError("illegal type in '%s'; must be a string, unicode, or a Variable" % name)
  89.         
  90.     else:
  91.         raise SPARQLError("'%s' argument must be a string, a Variable, or a list of those" % name)
  92.     return type(variables) == tuple
  93.  
  94.  
  95. def _createInitialBindings(pattern):
  96.     '''Creates an initial binding directory for the Graph Pattern by putting a None as a value for each
  97.     query variable.
  98.  
  99.     @param pattern: graph pattern
  100.     @type pattern: L{GraphPattern<rdflib.sparql.GraphPattern>}
  101.     '''
  102.     bindings = { }
  103.     for c in pattern.unbounds:
  104.         bindings[c] = None
  105.     
  106.     return bindings
  107.  
  108.  
  109. class _SPARQLNode:
  110.     """
  111.     The SPARQL implementation is based on the creation of a tree, each
  112.     level for each statement in the 'where' clause of SPARQL.
  113.  
  114.     Each node maintains a 'binding' dictionary, with the variable
  115.     names and either a None if not yet bound, or the binding
  116.     itself. The method 'expand' tries to make one more step of binding
  117.     by looking at the next statement: it takes the statement of the
  118.     current node, binds the variables if there is already a binding,
  119.     and looks at the triple store for the possibilities. If it finds
  120.     valid new triplets, that will bind some more variables, and
  121.     children will be created with the next statement in the 'where'
  122.     array with a new level of bindings. This is done for each triplet
  123.     found in the store, thereby branching off the tree. If all
  124.     variables are already bound but the statement, with the bound
  125.     variables, is not 'true' (ie, there is no such triple in the
  126.     store), the node is marked as 'clash' and no more expansion is
  127.     made; this node will then be thrown away by the parent. If I{all}
  128.     children of a node is a clash, then it is marked as a clash
  129.     itself.
  130.  
  131.     At the end of the process, the leaves of the tree are searched; if
  132.     a leaf is such that:
  133.  
  134.       - all variables are bound
  135.       - there is no clash
  136.  
  137.     then the bindings are returned as possible answers to the query.
  138.  
  139.     The optional clauses are treated separately: each 'valid' leaf is
  140.     assigned an array of expansion trees that contain the optional
  141.     clauses (that may have some unbound variables bound at the leaf,
  142.     though).
  143.  
  144.     @ivar parent: parent in the tree
  145.     @type parent: _SPARQLNode
  146.     @ivar children: the children (in an array)
  147.     @type children: array of _SPARQLNode
  148.     @ivar bindings:  copy of the bindings locally
  149.     @type bindings: dictionary
  150.     @ivar statement:  the current statement
  151.     @type statement: a (s,p,o,f) tuple ('f' is the local filter or None)
  152.     @ivar rest:  the rest of the statements (an array)
  153.     @ivar clash: intialized to False
  154.     @type clash: Boolean
  155.     @ivar bound:  True or False depending on whether all variables are bound in self.binding
  156.     @type bound: Boolean
  157.     @ivar optionalTrees: expansion trees for optional statements
  158.     @type optionalTrees: array of _SPARQLNode instances
  159.     """
  160.     
  161.     def __init__(self, parent, bindings, statements, tripleStore):
  162.         """
  163.         @param parent:     parent node
  164.         @param bindings:   a dictionary with the bindings that are already done or with None value if no binding yet
  165.         @param statements: array of statements from the 'where' clause. The first element is
  166.         for the current node, the rest for the children. If empty, then no
  167.         expansion occurs (ie, the node is a leaf)
  168.         @param tripleStore: the 'owner' triple store
  169.         @type tripleStore: L{sparqlGraph<rdflib.sparql.sparqlGraph.sparqlGraph>}
  170.         """
  171.         self.tripleStore = tripleStore
  172.         self.bindings = bindings
  173.         self.optionalTrees = []
  174.         if None in bindings.values():
  175.             self.bound = False
  176.         else:
  177.             self.bound = True
  178.         self.clash = False
  179.         self.parent = parent
  180.         self.children = []
  181.         if len(statements) > 0:
  182.             self.statement = statements[0]
  183.             self.rest = statements[1:]
  184.         else:
  185.             self.statement = None
  186.             self.rest = None
  187.  
  188.     
  189.     def returnResult(self, select):
  190.         """
  191.         Collect the result by search the leaves of the the tree. The
  192.         variables in the select are exchanged against their bound
  193.         equivalent (if applicable). This action is done on the valid
  194.         leaf nodes only, the intermediate nodes only gather the
  195.         children's results and combine it in one array.
  196.  
  197.         @param select: the array of unbound variables in the original
  198.         select that do not appear in any of the optionals. If None,
  199.         the full binding should be considered (this is the case for
  200.         the SELECT * feature of SPARQL)
  201.         @returns: an array of dictionaries with non-None bindings.
  202.         """
  203.         if len(self.children) > 0:
  204.             retval = []
  205.             for c in self.children:
  206.                 res = c.returnResult(select)
  207.                 for t in res:
  208.                     retval.append(t)
  209.                 
  210.             
  211.             return retval
  212.         retval = []
  213.         if self.bound == True and self.clash == False:
  214.             result = { }
  215.             if select:
  216.                 for a in select:
  217.                     if a in self.bindings:
  218.                         result[a] = self.bindings[a]
  219.                         continue
  220.                     len(self.children) > 0
  221.                 
  222.             else:
  223.                 result = self.bindings.copy()
  224.             retval = [
  225.                 result]
  226.             for optTree in self.optionalTrees:
  227.                 optionals = optTree.returnResult(select)
  228.                 if len(optionals) == 0:
  229.                     continue
  230.                     continue
  231.                 if len(optionals) == 1:
  232.                     optResult = optionals[0]
  233.                     for res in retval:
  234.                         for k in optResult:
  235.                             if optResult[k] != None:
  236.                                 res[k] = optResult[k]
  237.                                 continue
  238.                         
  239.                     
  240.                 newRetval = []
  241.                 for optResult in optionals:
  242.                     for res in retval:
  243.                         dct = { }
  244.                         dct = res.copy()
  245.                         for k in optResult:
  246.                             if optResult[k] != None:
  247.                                 dct[k] = optResult[k]
  248.                                 continue
  249.                         
  250.                         newRetval.append(dct)
  251.                     
  252.                 
  253.                 retval = newRetval
  254.             
  255.         
  256.         return retval
  257.  
  258.     
  259.     def expandSubgraph(self, subTriples, pattern):
  260.         '''
  261.         Method used to collect the results. There are two ways to
  262.         invoke the method:
  263.  
  264.           - if the pattern argument is not None, then this means the
  265.           construction of a separate triple store with the
  266.           results. This means taking the bindings in the node, and
  267.           constuct the graph via the
  268.           L{construct<rdflib.sparql.graphPattern.GraphPattern.construct>}
  269.           method. This happens on the valid leafs; intermediate nodes
  270.           call the same method recursively - otherwise, a leaf returns
  271.           an array of the bindings, and intermediate methods aggregate
  272.           those.
  273.  
  274.         In both cases, leaf nodes may successifely expand the optional
  275.         trees that they may have.
  276.  
  277.         @param subTriples: the triples so far
  278.         @type subTriples: L{sparqlGraph<rdflib.sparql.sparqlGraph.sparqlGraph>}
  279.         @param pattern: a graph pattern used to construct a graph
  280.         @type pattern: L{GraphPattern<rdflib.sparql.graphPattern.GraphPattern>}
  281.         @return: if pattern is not None, an array of binding dictionaries
  282.         '''
  283.         
  284.         def b(r, bind):
  285.             if type(r) == str:
  286.                 val = bind[r]
  287.                 if val == None:
  288.                     raise RuntimeError()
  289.                 val == None
  290.                 return bind[r]
  291.             return r
  292.  
  293.         if len(self.children) > 0:
  294.             if pattern == None:
  295.                 retval = []([], [ x.expandSubgraph(subTriples, None) for x in self.children ], [])
  296.                 (s, p, o, func) = self.statement
  297.                 for bind in retval:
  298.                     
  299.                     try:
  300.                         st = (b(s, bind), b(p, bind), b(o, bind))
  301.                         subTriples.add(st)
  302.                     continue
  303.                     reduce
  304.                     continue
  305.  
  306.                 
  307.                 return retval
  308.             for x in self.children:
  309.                 x.expandSubgraph(subTriples, pattern)
  310.             
  311.         elif self.bound == True and self.clash == False:
  312.             for t in self.optionalTrees:
  313.                 t.expandSubgraph(subTriples, pattern)
  314.             
  315.             if pattern == None:
  316.                 return [
  317.                     self.bindings]
  318.             pattern.construct(subTriples, self.bindings)
  319.         else:
  320.             return []
  321.         return pattern == None
  322.  
  323.     
  324.     def _bind(self, r):
  325.         '''
  326.         @param r: string
  327.         @return: returns None if no bindings occured yet, the binding otherwise
  328.         '''
  329.         if isinstance(r, basestring) and not isinstance(r, Identifier):
  330.             if self.bindings[r] == None:
  331.                 return None
  332.             return self.bindings[r]
  333.         not isinstance(r, Identifier)
  334.         if isinstance(r, SessionBNode):
  335.             return r
  336.         if isinstance(r, BNode):
  337.             return self.bindings.get(r)
  338.         return r
  339.  
  340.     
  341.     def expand(self, constraints):
  342.         '''
  343.         The expansion itself. See class comments for details.
  344.  
  345.         @param constraints: array of global constraining (filter) methods
  346.         '''
  347.         if self.statement:
  348.             (s, p, o, func) = self.statement
  349.             search_s = self._bind(s)
  350.             search_p = self._bind(p)
  351.             search_o = self._bind(o)
  352.             if self.tripleStore.graphVariable:
  353.                 if not hasattr(self.tripleStore.graph, 'quads'):
  354.                     raise AssertionError, 'Graph graph patterns can only be used with Graph instances with a quad method'
  355.                 searchRT = self.tripleStore.graph.quads((search_s, search_p, search_o))
  356.             else:
  357.                 searchRT = self.tripleStore.graph.triples((search_s, search_p, search_o))
  358.             for tripleOrQuad in searchRT:
  359.                 if self.tripleStore.graphVariable:
  360.                     (result_s, result_p, result_o, parentGraph) = tripleOrQuad
  361.                 else:
  362.                     (result_s, result_p, result_o) = tripleOrQuad
  363.                 if func != None and func(result_s, result_p, result_o) == False:
  364.                     continue
  365.                 
  366.                 new_bindings = self.bindings.copy()
  367.                 if search_s == None:
  368.                     new_bindings[s] = result_s
  369.                 
  370.                 if search_p == None:
  371.                     new_bindings[p] = result_p
  372.                 
  373.                 if search_o == None:
  374.                     new_bindings[o] = result_o
  375.                 
  376.                 if self.tripleStore.graphVariable:
  377.                     new_bindings[self.tripleStore.graphVariable] = parentGraph.identifier
  378.                 
  379.                 child = _SPARQLNode(self, new_bindings, self.rest, self.tripleStore)
  380.                 child.expand(constraints)
  381.                 if self.clash == False:
  382.                     self.children.append(child)
  383.                     continue
  384.             
  385.             if len(self.children) == 0:
  386.                 self.clash = True
  387.             
  388.         elif self.bound == True and self.clash == False:
  389.             for func in constraints:
  390.                 if func(self.bindings) == False:
  391.                     self.clash = True
  392.                     break
  393.                     continue
  394.             
  395.         
  396.  
  397.     
  398.     def expandOptions(self, bindings, statements, constraints):
  399.         """
  400.         Managing optional statements. These affect leaf nodes only, if
  401.         they contain 'real' results. A separate Expansion tree is
  402.         appended to such a node, one for each optional call.
  403.  
  404.         @param bindings: current bindings dictionary
  405.  
  406.         @param statements: array of statements from the 'where'
  407.         clause. The first element is for the current node, the rest
  408.         for the children. If empty, then no expansion occurs (ie, the
  409.         node is a leaf). The bindings at this node are taken into
  410.         account (replacing the unbound variables with the real
  411.         resources) before expansion
  412.  
  413.         @param constraints: array of constraint (filter) methods
  414.         """
  415.         
  416.         def replace(key, resource, tupl):
  417.             (s, p, o, func) = tupl
  418.             if key == s:
  419.                 s = resource
  420.             
  421.             if key == p:
  422.                 p = resource
  423.             
  424.             if key == o:
  425.                 o = resource
  426.             
  427.             return (s, p, o, func)
  428.  
  429.         if len(self.children) == 0:
  430.             if self.bound == True and self.clash == False:
  431.                 toldBNodeLookup = { }
  432.                 for key in self.bindings:
  433.                     normalizedStatements = []
  434.                     for t in statements:
  435.                         val = self.bindings[key]
  436.                         if isinstance(val, BNode) and val not in toldBNodeLookup:
  437.                             toldBNodeLookup[val] = val
  438.                         
  439.                         normalizedStatements.append(replace(key, self.bindings[key], t))
  440.                     
  441.                     statements = normalizedStatements
  442.                     if key in bindings:
  443.                         del bindings[key]
  444.                         continue
  445.                 
  446.                 bindings.update(toldBNodeLookup)
  447.                 optTree = _SPARQLNode(None, bindings, statements, self.tripleStore)
  448.                 self.optionalTrees.append(optTree)
  449.                 optTree.expand(constraints)
  450.             
  451.         else:
  452.             for c in self.children:
  453.                 c.expandOptions(bindings, statements, constraints)
  454.             
  455.  
  456.  
  457.  
  458. def _processResults(select, arr):
  459.     '''
  460.     The result in an expansion node is in the form of an array of
  461.     binding dictionaries.  The caller should receive an array of
  462.     tuples, each tuple representing the final binding (or None) I{in
  463.     the order of the original select}. This method is the last step of
  464.     processing by processing these values to produce the right result.
  465.  
  466.     @param select: the original selection list. If None, then the
  467.     binding should be taken as a whole (this corresponds to the SELECT * feature of SPARQL)
  468.     @param arr: the array of bindings
  469.     @type arr:
  470.     an array of dictionaries
  471.     @return: a list of tuples with the selection results
  472.     '''
  473.     retval = []
  474.     return retval
  475.  
  476.  
  477. def query(graph, selection, patterns, optionalPatterns = [], initialBindings = { }):
  478.     '''
  479.     A shorthand for the creation of a L{Query} instance, returning
  480.     the result of a L{Query.select} right away. Good for most of
  481.     the usage, when no more action (clustering, etc) is required.
  482.  
  483.     @param selection: a list or tuple with the selection criteria,
  484.     or a single string. Each entry is a string that begins with a"?".
  485.  
  486.     @param patterns: either a
  487.     L{GraphPattern<rdflib.sparql.graphPattern.GraphPattern>}
  488.     instance or a list of instances thereof. Each pattern in the
  489.     list represent an \'OR\' (or \'UNION\') branch in SPARQL.
  490.  
  491.     @param optionalPatterns: either a
  492.     L{GraphPattern<rdflib.sparql.graphPattern.GraphPattern>}
  493.     instance or a list of instances thereof. For each elements in
  494.     the \'patterns\' parameter is combined with each of the optional
  495.     patterns and the results are concatenated. The list may be
  496.     empty.
  497.  
  498.     @return: list of query results
  499.     @rtype: list of tuples
  500.     '''
  501.     result = queryObject(graph, patterns, optionalPatterns, initialBindings)
  502.     if result == None:
  503.         msg = 'Errors in the patterns, no valid query object generated; '
  504.         if isinstance(patterns, GraphPattern):
  505.             msg += 'pattern:\n%s' % patterns
  506.         else:
  507.             msg += 'pattern:\n%s\netc...' % patterns[0]
  508.         raise SPARQLError(msg)
  509.     result == None
  510.     return result.select(selection)
  511.  
  512.  
  513. def queryObject(graph, patterns, optionalPatterns = [], initialBindings = None):
  514.     """
  515.     Creation of a L{Query} instance.
  516.  
  517.     @param patterns: either a
  518.     L{GraphPattern<rdflib.sparql.graphPattern.GraphPattern>}
  519.     instance or a list of instances thereof. Each pattern in the
  520.     list represent an 'OR' (or 'UNION') branch in SPARQL.
  521.  
  522.     @param optionalPatterns: either a
  523.     L{GraphPattern<rdflib.sparql.graphPattern.GraphPattern>}
  524.     instance or a list of instances thereof. For each elements in
  525.     the 'patterns' parameter is combined with each of the optional
  526.     patterns and the results are concatenated. The list may be
  527.     empty.
  528.  
  529.     @return: Query object
  530.     @rtype: L{Query}
  531.     """
  532.     
  533.     def checkArg(arg, error):
  534.         if arg == None:
  535.             return []
  536.         if isinstance(arg, GraphPattern):
  537.             return [
  538.                 arg]
  539.         if type(arg) == list or type(arg) == tuple:
  540.             for p in arg:
  541.                 if not isinstance(p, GraphPattern):
  542.                     raise SPARQLError("'%s' argument must be a GraphPattern or a list of those" % error)
  543.                 isinstance(p, GraphPattern)
  544.             
  545.             return arg
  546.         raise SPARQLError("'%s' argument must be a GraphPattern or a list of those" % error)
  547.  
  548.     finalPatterns = checkArg(patterns, 'patterns')
  549.     finalOptionalPatterns = checkArg(optionalPatterns, 'optionalPatterns')
  550.     retval = None
  551.     if not initialBindings:
  552.         initialBinding = { }
  553.     
  554.     for pattern in finalPatterns:
  555.         _checkOptionals(pattern, finalOptionalPatterns)
  556.         bindings = _createInitialBindings(pattern)
  557.         if initialBindings:
  558.             bindings.update(initialBindings)
  559.         
  560.         top = _SPARQLNode(None, bindings, pattern.patterns, graph)
  561.         top.expand(pattern.constraints)
  562.         for opt in finalOptionalPatterns:
  563.             bindings = _createInitialBindings(opt)
  564.             if initialBindings:
  565.                 bindings.update(initialBindings)
  566.             
  567.             top.expandOptions(bindings, opt.patterns, opt.constraints)
  568.         
  569.         r = Query(top, graph)
  570.         if retval == None:
  571.             retval = r
  572.             continue
  573.         retval = retval + r
  574.     
  575.     return retval
  576.  
  577.  
  578. class Query:
  579.     '''
  580.     Result of a SPARQL query. It stores to the top of the query tree, and allows some subsequent
  581.     inquiries on the expanded tree. B{This class should not be
  582.     instantiated by the user,} it is done by the L{queryObject<SPARQL.queryObject>} method.
  583.  
  584.     '''
  585.     
  586.     def __init__(self, sparqlnode, triples, parent1 = None, parent2 = None):
  587.         '''
  588.         @param sparqlnode: top of the expansion tree
  589.         @type sparqlnode: _SPARQLNode
  590.         @param triples: triple store
  591.         @type triples: L{sparqlGraph<rdflib.sparql.sparqlGraph>}
  592.         @param parent1: possible parent Query when queries are combined by summing them up
  593.         @type parent1: L{Query}
  594.         @param parent2: possible parent Query when queries are combined by summing them up
  595.         @type parent2: L{Query}
  596.         '''
  597.         self.top = sparqlnode
  598.         self.triples = triples
  599.         self.parent1 = parent1
  600.         self.parent2 = parent2
  601.  
  602.     
  603.     def __add__(self, other):
  604.         """This may be useful when several queries are performed and
  605.         one wants the 'union' of those.  Caveat: the triple store must
  606.         be the same for each argument. This method is used internally
  607.         only anyway...  Efficiency trick (I hope it works): the
  608.         various additions on subgraphs are not done here; the results
  609.         are calculated only if really necessary, ie, in a lazy
  610.         evaluation manner.  This is achieved by storing self and the
  611.         'other' in the new object
  612.         """
  613.         return Query(None, self.triples, self, other)
  614.  
  615.     
  616.     def _getFullBinding(self):
  617.         '''Retrieve the full binding, ie, an array of binding dictionaries
  618.         '''
  619.         if self.parent1 != None and self.parent2 != None:
  620.             return self.parent1._getFullBinding() + self.parent2._getFullBinding()
  621.         return self.top.returnResult(None)
  622.  
  623.     
  624.     def _getAllVariables(self):
  625.         '''Retrieve the list of all variables, to be returned'''
  626.         if self.parent1 and self.parent2:
  627.             return list2set(self.parent1._getAllVariables() + self.parent2._getAllVariables())
  628.         return list2set(self.top.bindings.keys())
  629.  
  630.     
  631.     def _orderedSelect(self, selection, orderedBy, orderDirection):
  632.         '''
  633.         The variant of the selection (as below) that also includes the sorting. Because that is much less efficient, this is
  634.         separated into a distinct method that is called only if necessary. It is called from the L{select<select>} method.
  635. \t\t
  636.         Because order can be made on variables that are not part of the final selection, this method retrieves a I{full}
  637.         binding from the result to be able to order it (whereas the core L{select<select>} method retrieves from the result
  638.         the selected bindings only). The full binding is an array of (binding) dictionaries; the sorting sorts this array
  639.         by comparing the bound variables in the respective dictionaries. Once this is done, the final selection is done.
  640.  
  641.         @param selection: Either a single query string, or an array or tuple thereof.
  642.         @param orderBy: either a function or a list of strings (corresponding to variables in the query). If None, no sorting occurs
  643.         on the results. If the parameter is a function, it must take two dictionary arguments (the binding dictionaries), return
  644.         -1, 0, and 1, corresponding to smaller, equal, and greater, respectively.
  645.         @param orderDirection: if not None, then an array of integers of the same length as orderBy, with values the constants
  646.         ASC or DESC (defined in the module). If None, an ascending order is used.
  647.         @return: selection results
  648.         @rtype: list of tuples
  649.         @raise SPARQLError: invalid sorting arguments
  650.         '''
  651.         fullBinding = self._getFullBinding()
  652.         fullBinding.sort(_sortBinding)
  653.         retval = _processResults(selection, fullBinding)
  654.         return retval
  655.  
  656.     
  657.     def select(self, selection, distinct = True, limit = None, orderBy = None, orderAscend = None, offset = 0):
  658.         """
  659.         Run a selection on the query.
  660.  
  661.         @param selection: Either a single query string, or an array or tuple thereof.
  662.         @param distinct: if True, identical results are filtered out
  663.         @type distinct: Boolean
  664.         @param limit: if set to an integer value, the first 'limit' number of results are returned; all of them otherwise
  665.         @type limit: non negative integer
  666.         @param orderBy: either a function or a list of strings (corresponding to variables in the query). If None, no sorting occurs
  667.         on the results. If the parameter is a function, it must take two dictionary arguments (the binding dictionaries), return
  668.         -1, 0, and 1, corresponding to smaller, equal, and greater, respectively.
  669.         @param orderAscend: if not None, then an array of booelans of the same length as orderBy, True for ascending and False
  670. \t\tfor descending. If None, an ascending order is used.
  671.         @offset the starting point of return values in the array of results. Obviously, this parameter makes real sense if
  672.         some sort of order is defined.
  673.         @return: selection results
  674.         @rtype: list of tuples
  675.         @raise SPARQLError: invalid selection argument
  676.         """
  677.         
  678.         def _uniquefyList(lst):
  679.             '''Return a copy of the list but possible duplicate elements are taken out. Used to
  680.             post-process the outcome of the query
  681.             @param lst: input list
  682.             @return: result list
  683.             '''
  684.             if len(lst) <= 1:
  685.                 return lst
  686.             if orderBy != None:
  687.                 retval = []
  688.                 for i in xrange(0, len(lst)):
  689.                     v = lst[i]
  690.                     skip = False
  691.                     for w in retval:
  692.                         if w == v:
  693.                             skip = True
  694.                             break
  695.                             continue
  696.                         len(lst) <= 1
  697.                     
  698.                     if not skip:
  699.                         retval.append(v)
  700.                         continue
  701.                 
  702.                 return retval
  703.             return list(sets.Set(lst))
  704.  
  705.         selectionF = _variablesToArray(selection, 'selection')
  706.         if type(offset) is not types.IntType or offset < 0:
  707.             raise SPARQLError("'offset' argument is invalid")
  708.         offset < 0
  709.         if limit != None:
  710.             if type(limit) is not types.IntType or limit < 0:
  711.                 raise SPARQLError("'offset' argument is invalid")
  712.             limit < 0
  713.         
  714.         if orderBy != None:
  715.             results = self._orderedSelect(selectionF, orderBy, orderAscend)
  716.         elif self.parent1 != None and self.parent2 != None:
  717.             results = self.parent1.select(selectionF) + self.parent2.select(selectionF)
  718.         else:
  719.             results = _processResults(selectionF, self.top.returnResult(selectionF))
  720.         if distinct:
  721.             retval = _uniquefyList(results)
  722.         else:
  723.             retval = results
  724.         if limit != None:
  725.             return retval[offset:limit + offset]
  726.         if offset > 0:
  727.             return retval[offset:]
  728.         return retval
  729.  
  730.     
  731.     def construct(self, pattern = None):
  732.         '''
  733.         Expand the subgraph based on the pattern or, if None, the
  734.         internal bindings.
  735.  
  736.         In the former case the binding is used to instantiate the
  737.         triplets in the patterns; in the latter, the original
  738.         statements are used as patterns.
  739.  
  740.         The result is a separate triple store containing the subgraph.
  741.  
  742.         @param pattern: a L{GraphPattern<rdflib.sparql.graphPattern.GraphPattern>} instance or None
  743.         @return: a new triple store
  744.         @rtype: L{sparqlGraph<rdflib.sparql.sparqlGraph>}
  745.         '''
  746.         if self.parent1 != None and self.parent2 != None:
  747.             return self.parent1.construct(pattern) + self.parent2.construct(pattern)
  748.         subgraph = SPARQLGraph()
  749.         self.top.expandSubgraph(subgraph, pattern)
  750.         return subgraph
  751.  
  752.     
  753.     def ask(self):
  754.         '''
  755.         Whether a specific pattern has a solution or not.
  756.         @rtype: Boolean
  757.         '''
  758.         return len(self.select('*')) != 0
  759.  
  760.     
  761.     def clusterForward(self, selection):
  762.         '''
  763.         Forward clustering, using all the results of the query as
  764.         seeds (when appropriate). It is based on the usage of the
  765.         L{cluster forward<rdflib.sparql.sparqlGraph.clusterForward>}
  766.         method for triple store.
  767.  
  768.         @param selection: a selection to define the seeds for
  769.         clustering via the selection; the result of select used for
  770.         the clustering seed
  771.  
  772.         @return: a new triple store
  773.         @rtype: L{sparqlGraph<rdflib.sparql.sparqlGraph>}
  774.         '''
  775.         if self.parent1 != None and self.parent2 != None:
  776.             return self.parent1.clusterForward(selection) + self.parent2.clusterForward(selection)
  777.         clusterF = SPARQLGraph()
  778.         for r in reduce((lambda x, y: list(x) + list(y)), self.select(selection), ()):
  779.             
  780.             try:
  781.                 check_subject(r)
  782.                 self.triples.clusterForward(r, clusterF)
  783.             continue
  784.             continue
  785.             continue
  786.  
  787.         
  788.         return clusterF
  789.  
  790.     
  791.     def clusterBackward(self, selection):
  792.         '''
  793.         Backward clustering, using all the results of the query as
  794.         seeds (when appropriate). It is based on the usage of the
  795.         L{cluster backward<rdflib.sparql.sparqlGraph.clusterBackward>}
  796.         method for triple store.
  797.  
  798.         @param selection: a selection to define the seeds for
  799.         clustering via the selection; the result of select used for
  800.         the clustering seed
  801.  
  802.         @return: a new triple store
  803.         @rtype: L{sparqlGraph<rdflib.sparql.sparqlGraph>}
  804.         '''
  805.         if self.parent1 != None and self.parent2 != None:
  806.             return self.parent1.clusterBackward(selection) + self.parent2.clusterBackward(selection)
  807.         clusterB = SPARQLGraph()
  808.         for r in reduce((lambda x, y: list(x) + list(y)), self.select(selection), ()):
  809.             self.triples.clusterBackward(r, clusterB)
  810.         
  811.         return clusterB
  812.  
  813.     
  814.     def cluster(self, selection):
  815.         '''
  816.         Cluster: a combination of L{Query.clusterBackward} and
  817.         L{Query.clusterForward}.  @param selection: a selection to
  818.         define the seeds for clustering via the selection; the result
  819.         of select used for the clustering seed
  820.         '''
  821.         return self.clusterBackward(selection) + self.clusterForward(selection)
  822.  
  823.     
  824.     def describe(self, selection, forward = True, backward = True):
  825.         '''
  826.         The DESCRIBE Form in the SPARQL draft is still in state of
  827.         flux, so this is just a temporary method, in fact.  It may not
  828.         correspond to what the final version of describe will be (if
  829.         it stays in the draft at all, that is).  At present, it is
  830.         simply a wrapper around L{cluster}.
  831.  
  832.         @param selection: a selection to define the seeds for
  833.         clustering via the selection; the result of select used for
  834.         the clustering seed
  835.  
  836.         @param forward: cluster forward yes or no
  837.         @type forward: Boolean
  838.         @param backward: cluster backward yes or no
  839.         @type backward: Boolean
  840.         '''
  841.         if forward and backward:
  842.             return self.cluster(selection)
  843.         if forward:
  844.             return self.clusterForward(selection)
  845.         if backward:
  846.             return self.clusterBackward(selection)
  847.         return SPARQLGraph()
  848.  
  849.  
  850.